package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.VisitorLogDao;
import com.jbp.model.User;
import com.jbp.model.VisitorLog;
import com.jbp.service.UserLevelService;
import com.jbp.service.UserService;
import com.jbp.service.VisitorLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import sun.reflect.generics.visitor.Visitor;

import java.util.Date;
import java.util.Map;

/**
 * 访客记录(VisitorLog)表服务实现类
 * @author cc
 * @since 2020-06-21 22:46:33
 */
@Service
public class VisitorLogServiceImpl extends ServiceImpl<VisitorLogDao, VisitorLog> implements VisitorLogService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLevelService userLevelService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<VisitorLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", params.get("userId"));
        queryWrapper.orderByDesc("create_time");

        IPage<VisitorLog> page = this.page(
                new Query<VisitorLog>().getPage(params),
                queryWrapper
        );

        page.getRecords().forEach(record->{
            User friend = userService.getById(record.getFriendId());
            if (friend != null) {
                record.setFriendName(friend.getName());
                record.setFriendImg(friend.getImg());
                record.setFriendSex(friend.getSex());
                record.setFriendLevel(friend.getLevel());
                record.setFriendLevelOn(userLevelService.levelOn(friend.getId()));
            }
        });

        return new PageUtils(page);
    }

    @Override
    public PageUtils whoVisitMe(Map<String, Object> params) {
        QueryWrapper<VisitorLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("friend_id", params.get("userId"));
        queryWrapper.orderByDesc("create_time");

        IPage<VisitorLog> page = this.page(
                new Query<VisitorLog>().getPage(params),
                queryWrapper
        );

        page.getRecords().forEach(record->{
            User friend = userService.getById(record.getUserId());
            if (friend != null) {
                record.setFriendName(friend.getName());
                record.setFriendImg(friend.getImg());
                record.setFriendSex(friend.getSex());
                record.setFriendLevel(friend.getLevel());
                record.setFriendLevelOn(userLevelService.levelOn(friend.getId()));
            }
        });

        return new PageUtils(page);
    }

    //增加访客记录
    @Override
    public void addLog(Integer userId, Integer friendId) {
        VisitorLog visitorLog = getOne(new QueryWrapper<VisitorLog>()
                .eq("user_id", userId)
                .eq("friend_id", friendId)
        );
        if (visitorLog==null) {
            visitorLog = new VisitorLog();
            visitorLog.setUserId(userId);
            visitorLog.setFriendId(friendId);
            save(visitorLog);
        } else {
            visitorLog.setCreateTime(new Date());
            updateById(visitorLog);
        }
    }
}